/**
 * Created by Administrator on 2014/9/7.
 */
function changPasswd(){

    if(checkOldPasswd()&&checkNewPasswd()&&confirmPasswd()){
        return true;
    }else{
        return false;
    }
}

function checkOldPasswd() {
    var flag = false;
    var passwd = document.getElementById("oldPasswd");
    if (passwd.value == null || passwd.value.trim() == "") {
        flag = false;
        $("#oldpasswdmsg").html("密码不能为空")
        passwd.focus();
    }else{
        $.ajax({
            url: "/Origami/checkOldPasswd",
            type: "post",
            data: {oldPasswd: passwd.value},
            dataType: "json",
            async: false,
            complete: function (data) {
                if (data.responseText == 'no') {
                    flag = false;
                    $("#oldpasswdmsg").html("密码错误")
                    passwd.focus();
                } else if (data.responseText == 'error') {
                    flag = false;
                    $('#failAlert').show();
                } else {
                    flag = true;
                    $("#oldpasswdmsg").html("")
                }
            }
        })
    }
    return flag;
}
function checkNewPasswd(){
    var flag = false;
    var passwd = document.getElementById("newPasswd");
    if (passwd.value == null || passwd.value.trim() == "") {
        flag = false;
        $("#newPasswdmsg").html("密码不能为空")
        passwd.focus();
    } else if (passwd.value.length < 6 || passwd.value.length > 10) {
        flag = false;
        $("#newPasswdmsg").html("请输入6-10位的密码")
        passwd.focus();
    } else {
        flag = true;
        $("#newPasswdmsg").html("")
    }
    return flag;
}
function confirmPasswd() {
    var flag = false;
    var passwd1 = document.getElementById("newPasswd");
    var passwd2 = document.getElementById("confnewPasswd");
    if (passwd2.value == null || passwd2.value.trim() == "") {
        flag = false;
        $("#confnewPasswdmsg").html("请再次输入密码")
    }
    else if (passwd1.value != passwd2.value) {
        flag = false;
        $("#confnewPasswdmsg").html("两次输入的密码不一致");
    } else {
        flag = true;
        $("#confnewPasswdmsg").html("");
    }
    return flag;
}

$(function () {
    $("#file_upload_1").uploadify({
        height: 30,
        buttonImage: "",
        swf: 'uploadify/uploadify.swf',
        uploader: 'uploadHeadImage',
        width: 120,
        fileObjName: 'uploadfile',
        progressData: 'speed',
        multi: false,
        preventCaching: false,
        auto: false,
        fileTypeDesc: '不超过2M的图片 (*.gif;*.jpg;*.png)',
        fileTypeExts: '*.gif;*.jpg;*.jpeg;*.png',
        cancelImg: 'uploadify/uploadify-cancel.png',//取消图片路径
        fileSizeLimit: 2048,  //允许上传的文件大小(kb)  此为2M
        uploadLimit: 999,
        buttonText: '浏览',
        onUploadError: function (file, errorCode, errorMsg, errorString) {
            alert('图片 ' + file.name + ' 上传失败: ' + errorString);
        },
        onUploadSuccess: function (file, data, response) {
            document.getElementById("titleImg").setAttribute("src", data);
            document.getElementById("preview48").setAttribute("src", data);
            document.getElementById("preview96").setAttribute("src", data);
            document.getElementById("oldpath").setAttribute("value", data);
            document.getElementById("imgtable").style.display = "";
            jQuery(function ($) {
                // Create variables (in this scope) to hold the API and image size
                var jcrop_api, boundx, boundy;

                $("#titleImg").Jcrop({
                    onChange: updatePreview,
                    onSelect: updatePreview,
                    aspectRatio: 1
                }, function () {
                    // Use the API to get the real image size
                    var bounds = this.getBounds();
                    boundx = bounds[0];
                    boundy = bounds[1];
                    // Store the API in the jcrop_api variable
                    jcrop_api = this;
                    jcrop_api.animateTo([100, 100, 400, 300]);
                });

                function updatePreview(c) {
                    if (parseInt(c.w) > 0) {
                        var rx48 = 48 / c.w;
                        var ry48 = 48 / c.h;
                        var rx96 = 96 / c.w;
                        var ry96 = 96 / c.h;

                        $("#preview48").css({
                            width: Math.round(rx48 * boundx) + "px",
                            height: Math.round(ry48 * boundy) + "px",
                            marginLeft: "-" + Math.round(rx48 * c.x) + "px",
                            marginTop: "-" + Math.round(ry48 * c.y) + "px"
                        });
                        $("#preview96").css({
                            width: Math.round(rx96 * boundx) + "px",
                            height: Math.round(ry96 * boundy) + "px",
                            marginLeft: "-" + Math.round(rx96 * c.x) + "px",
                            marginTop: "-" + Math.round(ry96 * c.y) + "px"
                        });
                        $("#x1").val(c.x);
                        $("#y1").val(c.y);
                        $("#w").val(c.w);
                        $("#h").val(c.h);
                    }
                    ;
                };
            });

        }
    });
});


function thumbnailimage() {
    $.ajax({
        url: "/Origami/thumbnailHeadImage",
        type: "post",
        data: {x1: $("#x1").val(), y1: $("#y1").val(), w: $("#w").val(), h: $("#h").val(), oldpath: $("#oldpath").val()},
        dataType: "json",
        complete: function (data) {
            if (data.responseText == "success") {
                window.location.href="/Origami/info"
            } else {

                /*$("#savemsg").html("图片保存成功")
                 $("#saveimgmsg").html("");
                 $("#imgsrc").val(data.responseText);*/
                //window.location.href = "/Origami/info"
                $("#savemsg").html("系统错误保存失败");
            }
        }
    })
}

function saveprofile(){
    $.ajax({
        url: "/Origami/updateProfile",
        type: "post",
        data: {profile: $("#introduce").val()},
        dataType: "json",
        complete: function (data) {
            if (data.responseText == "success") {
                $("#successAlert").show();
            } else {
                $("#failAlert").show();
            }
        }
    })
}
